package com.example.superoa.model.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.superoa.common.Shijian;
import com.example.superoa.model.dao.KqBanciDao;
import com.example.superoa.model.mappers.BanCiMapper;
import com.example.superoa.model.pojos.KqBanci;
import com.mysql.cj.xdevapi.JsonArray;

@Service
@Transactional(rollbackFor=Exception.class)
public class BanCiService {
	@Autowired
	private KqBanciDao dao;
	
	@Autowired
	private BanCiMapper map;
	
	private Shijian sj=new Shijian();
	
	public JSONArray selectAll(){
		return tihuan(map.selectAll());
	}
	public String addpaichong(KqBanci banci,Integer bid) {
		System.out.println("---"+bid);
		JSONArray json;
		if(bid!=null) {
			json=map.selectAll1(bid);
		}else {
			json=map.selectAll();
		}
		for (Object object : json) {
			JSONObject obj=(JSONObject)object;
			if(obj.getString("b_name").equals(banci.getBName())) {
				return "有相同名称的班次";
			}else {
				if(sj.getPanduanShi1(banci.getBDao(), obj.getDate("b_dao"))==0&&sj.getPanduanShi1(banci.getBTui(), obj.getDate("b_tui"))==0) {
					return "有相同时间的班次";
				}
			}
		}
		return "";
	}
	public void addBanCi(KqBanci b) {
		if(b.getBQi()==1) {
			for (Object o : map.selectAll()) {
				JSONObject j=(JSONObject) o;
				KqBanci banci=dao.findById(Integer.parseInt(j.get("b_id").toString())).get();
				banci.setBQi(0);
			}
		}
		dao.save(b);
	}
	public JSONObject selectId(int id) {
		return  map.selectId(id);
	}
	public JSONArray tihuan(JSONArray json) {
		for (Object object : json) {
			JSONObject j=(JSONObject) object;
			String str="";
			if(j.get("b_qi").equals(1)) {
				str="启用中";
			}else {
				str="未启用";
			}
			j.put("b_qi", str);
		}
		return json;
	}
}
